package com.dycx.common.rabbitmq;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 消息配置类
 *
 * @author Liuhui
 * @since 2023-05-16
 **/
@Configuration
public class RabbitMqFactory {

    /*********************配置********************/
    @Bean(name = "amqpAdmin")
    public AmqpAdmin amqpAdmin(@Qualifier(value = "connectionFactory") ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    @Primary
    @Bean(name = "connectionFactory")
    public ConnectionFactory connectionFactory(RabbitMqConfig rabbitMqConfig) {
        CachingConnectionFactory rabbitConnectionFactory = new CachingConnectionFactory();
        rabbitConnectionFactory.setVirtualHost(rabbitMqConfig.getVirtualHost());
        rabbitConnectionFactory.setAddresses(rabbitMqConfig.getHost());
        rabbitConnectionFactory.setPort(rabbitMqConfig.getPort());
        rabbitConnectionFactory.setUsername(rabbitMqConfig.getUsername());
        rabbitConnectionFactory.setPassword(rabbitMqConfig.getPassword());
        return rabbitConnectionFactory;
    }

    /**
     * second生产者
     */
    @Bean(name = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(@Qualifier(value = "connectionFactory") ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    @Bean(name = "paymentListenerFactory")
    public SimpleRabbitListenerContainerFactory paymentListenerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier(value = "connectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        listenerContainerFactory.setPrefetchCount(1);
        configurer.configure(listenerContainerFactory, connectionFactory);
        return listenerContainerFactory;
    }


    @Bean(name = "messageListenerFactory")
    public SimpleRabbitListenerContainerFactory messageListenerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier(value = "connectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        listenerContainerFactory.setPrefetchCount(1);
        configurer.configure(listenerContainerFactory, connectionFactory);
        return listenerContainerFactory;
    }
}
